#!/usr/bin/env python3
"""
清理旧的单文件日志，迁移到新的分类结构
"""

import sys
from pathlib import Path

# 添加项目根目录到路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
sys.path.insert(0, str(project_root / "src"))

def cleanup_old_logs():
    """清理旧的日志文件"""
    log_dir = project_root / "logs"
    
    # 旧日志文件模式
    old_log_patterns = [
        "reactor_*.log",
        "status_*.log"
    ]
    
    old_files = []
    for pattern in old_log_patterns:
        old_files.extend(log_dir.glob(pattern))
    
    if not old_files:
        print("没有找到需要清理的旧日志文件")
        return
    
    print(f"找到 {len(old_files)} 个旧日志文件:")
    total_size = 0
    for file in old_files:
        size = file.stat().st_size
        total_size += size
        print(f"  - {file.name} ({size / 1024 / 1024:.2f} MB)")
    
    print(f"\n总大小: {total_size / 1024 / 1024:.2f} MB")
    
    confirm = input(f"\n是否要删除这些旧日志文件？(y/N): ")
    if confirm.lower() == 'y':
        for file in old_files:
            try:
                file.unlink()
                print(f"已删除: {file.name}")
            except Exception as e:
                print(f"删除失败 {file.name}: {e}")
        
        print(f"\n✅ 清理完成! 释放了 {total_size / 1024 / 1024:.2f} MB 空间")
        print("\n新的日志系统特点:")
        print("  📁 分类存储: app/ hardware/ web/ errors/")
        print("  🔄 自动轮转: 单文件最大10MB")
        print("  🔇 减少噪音: 硬件错误不再刷屏")
        print("  📊 结构化: 更易于分析和监控")
    else:
        print("已取消清理操作")


if __name__ == "__main__":
    cleanup_old_logs()
